package gp.app;

import genetic.GPApplication;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

@SuppressWarnings("serial")
public class GPRunner extends JFrame implements ActionListener, Observer {

	// GUI Elemente
	private JButton jb_selectConfigFile;
	private JLabel jl_configFile;
	private JButton jb_runOptimization;
	private JProgressBar jpb_progress;
	private JCheckBox jcb_showGPTree;
	private JCheckBox jcb_showGraph;
	private JLabel jl_memoryUsage;
	private JLabel jl_memoryUsageValue;
	
	// Views mit eigenem Fenster
	private GPTreeView gpview = null;
	private XYSeriesView gpgraph = null;
	
	// Applikations-Objekt
	private GPApplication app = new GPApplication();

	public GPApplication getApp() {
		return app;
	}

	public GPRunner() {
		super("GPRunner");
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {}
	    setLayout(new GridLayout(5,2));
	    
	    jb_selectConfigFile = new JButton("Select Config File");
	    jb_selectConfigFile.addActionListener(this);
	    
	    jl_configFile = new JLabel("(FileName)");
	    
	    jb_runOptimization = new JButton("Run Optimization");
	    jb_runOptimization.addActionListener(this);
	    jb_runOptimization.setEnabled(false);

	    jpb_progress = new JProgressBar();
	    
	    jcb_showGPTree = new JCheckBox("Show GP Tree");
	    jcb_showGPTree.addActionListener(this);
	    
	    jcb_showGraph = new JCheckBox("Show Graph");
	    jcb_showGraph.addActionListener(this);

	    jl_memoryUsage = new JLabel("Memory Usage: ");
	    jl_memoryUsageValue = new JLabel("xyz");
	    
	    getContentPane().add(jb_selectConfigFile);
	    getContentPane().add(jl_configFile);
	    getContentPane().add(jb_runOptimization);
	    getContentPane().add(jpb_progress);
	    getContentPane().add(jcb_showGPTree);
	    getContentPane().add(jcb_showGraph);
	    getContentPane().add(jl_memoryUsage);
	    getContentPane().add(jl_memoryUsageValue);
	    getContentPane().add(new JLabel("Statuszeile"));
	    
		// Windows-Listener
		addWindowListener(new WindowClosingAdapter(true));

		app.addObserver(this);
	}

	public void actionPerformed(ActionEvent event) {
		String cmd = event.getActionCommand();
		if (cmd.equals("Select Config File")) {
			JFileChooser jfc = new JFileChooser();

			int returnVal = jfc.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
            	jl_configFile.setText(jfc.getSelectedFile().getName());
            	app.loadSettingsFromFile(jfc.getSelectedFile());
				jb_runOptimization.setEnabled(true);
            } else {
        	    jb_runOptimization.setEnabled(false);
            }
        }
		else if (cmd.equals("Run Optimization")) {
			Thread t = new Thread(app);
			t.start();
		}
		else if (cmd.equals("Show GP Tree")) {
			if (gpview == null) {
				gpview = new GPTreeView(app);
			}
			if (jcb_showGPTree.isSelected()) {
				gpview.setVisible(true);
			}
			else {
				gpview.setVisible(false);
			}
		}
		else if (cmd.equals("Show Graph")) {
			if (gpgraph == null) {
				gpgraph = new XYSeriesView(app);
			}
			if (jcb_showGraph.isSelected()) {
				gpgraph.setVisible(true);
			}
			else {
				gpgraph.setVisible(false);
			}
		}
	}
	
	public static void main(String[] args) {
		GPRunner frame = new GPRunner();
		frame.setLocation(0, 0);
		frame.setResizable(false);
		frame.pack();
		frame.setVisible(true);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		jpb_progress.setValue(((GPApplication)arg0).getProgress());
		
	}
}